package com.yanxu;

import com.yanxu.domain.Event;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.serialization.SimpleStringEncoder;
import org.apache.flink.core.fs.Path;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.sink.filesystem.StreamingFileSink;
import org.apache.flink.streaming.api.functions.sink.filesystem.rollingpolicies.DefaultRollingPolicy;

import java.util.concurrent.TimeUnit;

/**
 * @author 折戟沉沙铁未销
 * @version V1.0
 * @date 2025-07-13-2025
 * @Description: api 学习
 *  sink 输出到file
 */
public class Api11_Sink_file {
    public static void main(String[] args) throws Exception {
        //获取执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        //源 -算子
        // 使用 addSource 自定义数据源
        DataStreamSource<Event> streamSource = env.fromElements(new Event("Mary", "./home", 1000L),
                new Event("Bob", "./cart", 2000L),
                new Event("Alice", "./prod?id=100", 3000L),
                new Event("Alice", "./prod?id=200", 3500L),
                new Event("Bob", "./prod?id=2", 2500L),
                new Event("Alice", "./prod?id=300", 3600L),
                new Event("Bob", "./home", 3000L),
                new Event("Bob", "./prod?id=1", 2300L),
                new Event("Bob", "./prod?id=3", 3300L));

        // 使用map 算子进行处理
        SingleOutputStreamOperator<String> mapOperator = streamSource.map(new MapFunction<Event, String>() {
            @Override
            public String map(Event event) throws Exception {
                return event.getName();
            }
        });

        StreamingFileSink<String> fileSink = StreamingFileSink.
                <String>forRowFormat(new Path("./output"), new SimpleStringEncoder<>("utf-8"))
                // 设置滚动策略（rolling policy），决定何时创建新文件
                .withRollingPolicy(
                        // 使用默认的滚动策略构建器进行配置
                        DefaultRollingPolicy.builder()
                                // 设置滚动间隔时间为15分钟，即每隔15分钟创建一个新的文件
                                .withRolloverInterval(TimeUnit.MINUTES.toMillis(15))
                                // 设置不活跃间隔时间，如果在这段时间内没有数据写入，也会触发新文件的创建
                                .withInactivityInterval(TimeUnit.MINUTES.toMillis(5))
                                // 设置单个文件的最大大小为1GB，达到此限制时会创建新文件
                                .withMaxPartSize(1024 * 1024 * 1024)
                                .build())
                .build();



        // sink 到文件中
        mapOperator.addSink(fileSink).setParallelism(1);


        //对环境进行执行。
        env.execute();
    }
}